package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.article.domain.Wenda;
import cn.wolfcode.luowowo.article.domain.WendaContent;
import cn.wolfcode.luowowo.article.service.IWendaContentService;
import cn.wolfcode.luowowo.article.service.IWendaService;
import cn.wolfcode.luowowo.common.query.QueryObject;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.website.annotation.UserParam;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.Date;


@Controller
@RequestMapping("/community")
public class CommunityController {

    @Reference
    private IWendaService wendaService;
    @Reference
    private IWendaContentService contentService;

    @RequestMapping("")
    public String index(Model model,QueryObject qo){
        PageInfo pageInfo = wendaService.query(qo);
        model.addAttribute("pageInfo",pageInfo);
        return "community/wenda";
    }

    @RequestMapping("save")
    //@ResponseBody
    public Object save(Model model,@UserParam UserInfo userInfo,Wenda wenda,WendaContent content){
        wenda.setCreateTime(new Date());
        wenda.setContent(content.getEditorValue());
        wenda.setNickname(userInfo.getNickname());
        wenda.setLevel(userInfo.getLevel());
        wenda.setHeadImge(userInfo.getHeadImgUrl());
        wenda.setReplynum(0);
        wenda.setViewnum(0);
        wenda.setSharenum(0);
        wenda.setThmbupnum(0);
        contentService.save(content);
        wendaService.save(wenda);
        return "redirect:/community";
    }

}
